﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.UserCode;
using System.Xml;
using System.Text;
using System.IO;
using System.Web.UI;
using Microsoft.SharePoint.WebPartPages;

namespace TSol.SandboxWebservice
{
    public partial class SandboxServicePageLayout : Microsoft.SharePoint.WebPartPages.WebPartPage
    {
        string webservicePartName = "";
        string soapResponse = "";
        string _request = "";
        string _response = "";
        MemoryStream stream1 = new MemoryStream();

        System.Web.UI.WebControls.WebParts.WebPart webservicePart = null;

        protected override void OnPreInit(EventArgs e)
        {
            webservicePartName = (string)Request.QueryString["op"] == null ? "" : (string)Request.QueryString["op"];
            if (webservicePartName != "")
            {
                this.MasterPageFile = "/_catalogs/masterpage/SandboxWebservice.master";
            }
            base.OnPreInit(e);
        }

        protected override void OnInit(EventArgs e)
        {
            if (webservicePartName != "")
            {
                SPLimitedWebPartManager manager = SPContext.Current.Web.GetLimitedWebPartManager(this.Context.Request.Url.AbsoluteUri,
                                                                                                    PersonalizationScope.User);
                if (manager.WebParts.Count > 0)
                {
                    foreach (System.Web.UI.WebControls.WebParts.WebPart part in manager.WebParts)
                    {
                        if (webservicePartName == "\"" + part.Title + "\"")
                        {
                            webservicePart = part;
                        }
                        else
                        {
                            part.Hidden = true;
                        }
                    }
                }
            }
            base.OnInit(e);
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (webservicePart != null)
            {
                Response.ClearContent();
                StringBuilder _stringBuilder = new StringBuilder();
                StringWriter _stringWriter = new StringWriter(_stringBuilder);
                HtmlTextWriter _htmlTextWriter = new HtmlTextWriter(_stringWriter);
                
                base.RenderChildren(_htmlTextWriter);

                string _webservicePartOutput = _stringBuilder.ToString();
                int first = _webservicePartOutput.IndexOf("<" + webservicePart.UniqueID + ">");
                int last = _webservicePartOutput.LastIndexOf("</" + webservicePart.UniqueID + ">");
                _webservicePartOutput = _webservicePartOutput.Substring(first, last - first + ("</" + webservicePart.UniqueID + ">").Length);

                XmlDocument doc = new XmlDocument();
                doc.LoadXml(_webservicePartOutput);
                            
                Response.Write(doc.FirstChild.FirstChild.InnerText);
            }
            else
            {
                base.Render(writer);
            }
        }
    }
}
